package com.ikun.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ikun.dao.SalariesDao;
import com.ikun.entity.Salaries;
import com.ikun.service.AttendancesService;
import com.ikun.service.SalariesService;

/**
 * @ClassName: SalariesServiceImpl
 * @Author: 吴思伟
 * @CreateTime: 2025-03-26 18:41
 * @Description: TODO
 */
@Service
public class SalariesServiceImpl implements SalariesService {
    private static final double BASE_SALARY = 100.0; // 基本工资

    @Autowired
    private SalariesDao salariesDao;

    @Autowired
    private AttendancesService attendancesService;

    @Override
    @Transactional // 开启事务保证两次操作必须一起完成
    public List<Salaries> calculateSalaryForManger(String formattedYearMonth, String manageId) {
        // // 计算考勤天数
        // int attendanceDays = attendanceService.calculateAttendanceDays(formattedYearMonth, manageId);
        // // 计算工资
        // double baseSalary = attendanceDays * BASE_SALARY; // 基本工资
        // double deduction = 0.0; // 减扣工资
        // double netSalary = baseSalary - deduction; // 实发工资
        // Salaries salary = new Salaries();
        // salary.setStaffId(Integer.parseInt(manageId));// 员工ID
        // salary.setPayMonth(formattedYearMonth);// 发放月份
        // salary.setBaseSalary(baseSalary);// 基本工资
        // salary.setDeduction(deduction); // 减扣工资
        // salary.setNetSalary(netSalary); // 实发工资
        // salary.setPayTime(new Date()); // 发放时间
        // // 保存工资记录
        // salariesDao.addSalary(salary);
        Salaries salaries = calculateSalary(formattedYearMonth, manageId);

        return salariesDao.calculateSalaryForManger(formattedYearMonth, manageId);
    }

    @Override
    public List<Salaries> calculateSalaryForMangerByName(String manageName) {
        return salariesDao.calculateSalaryForMangerByName(manageName);
    }

    @Override
    @Transactional // 开启事务保证两次操作必须一起完成
    public List<Salaries> calculateSalaryForStuff(String formattedYearMonth, String stuffId) {
        Salaries salaries = calculateSalary(formattedYearMonth, stuffId);

        return salariesDao.calculateSalaryForManger(formattedYearMonth, stuffId);
    }

    @Override
    public Salaries findSalaryByStaffIdAndPayMonth(Integer staffId, String payMonth) {
        return salariesDao.findSalaryByStaffIdAndPayMonth(staffId, payMonth);
    }

    @Transactional // 开启事务保证两次操作必须一起完成
    @Override
    public Salaries calculateSalary(String formattedYearMonth, String stuffId) {
        int attendanceDays = attendancesService.calculateAttendanceDays(formattedYearMonth, stuffId);
        // 计算工资
        double baseSalary = attendanceDays * BASE_SALARY; // 基本工资
        double deduction = 0.0; // 减扣工资
        double netSalary = baseSalary - deduction; // 实发工资
        Salaries salary = new Salaries();
        salary.setStaffId(Integer.parseInt(stuffId));// 员工ID
        salary.setPayMonth(formattedYearMonth);// 发放月份
        salary.setBaseSalary(baseSalary);// 基本工资
        salary.setDeduction(deduction); // 减扣工资
        salary.setNetSalary(netSalary); // 实发工资
        // salary.setPayTime(new Date()); // 发放时间
        // 保存工资记录
        if (salariesDao.findSalaryByStaffIdAndPayMonth(Integer.parseInt(stuffId), formattedYearMonth) == null) {
            salariesDao.addSalary(salary);
        } else {
            Integer sId =
                salariesDao.findSalaryByStaffIdAndPayMonth(Integer.parseInt(stuffId), formattedYearMonth).getSalaryId();
            salary.setSalaryId(sId);
            salariesDao.deleteSalary(Integer.parseInt(stuffId), formattedYearMonth);
            salariesDao.addSalary(salary);
        }
        return salary;
    }
}
